extends Control

export var is_phone_ctrl=false
var mov_ori=Vector2(0,0)
var mov_cur=Vector2(0,0)
var mov=false
var jmp=false
var mov_index=0
var mi=0
var ji=0

func _ready():
	if(OS.has_feature("Android")||OS.has_feature("iOS")):
		is_phone_ctrl=true
	visible=is_phone_ctrl

func _unhandled_input(event):
	if is_phone_ctrl:
		if event is InputEventScreenTouch:
			if event.pressed:
				if event.position.x<(get_viewport_rect().size.x)/2:
					mi=event.index
					mov_ori=event.position
					$"TouchscreenCurses".position=event.position
					$"TouchscreenCurses".show()
					mov=true
				else:
					ji=event.index
					jmp=true
			else:
				if event.index==mi:
					$"TouchscreenCurses".hide()
					mov=false
				if event.index==ji:
					jmp=false
		elif event is InputEventScreenDrag:
			if event.position.x<(get_viewport_rect().size.x)/2||event.index==mov_index:
				mov_index=event.index
				mov_cur=event.position

func _process(_delta):
	if is_phone_ctrl:
		i.keys.jump=jmp
		if mov:
			if mov_cur.x<mov_ori.x:
				i.keys.left=true
			else:
				i.keys.right=true
			i.keys.run=sqrt(pow((mov_ori.x-mov_cur.x),2)+pow((mov_ori.y-mov_cur.y),2))>128

func _on_x_pressed():
	Input.action_press("ui_cancel")

func _on_z_pressed():
	Input.action_press("ui_accept")

func _on_bag_pressed():
	Input.action_press("bag")
